/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.PrivilegeDao;
import com.rbh.risk.entity.ACL;
import com.rbh.risk.entity.Privilege;
import com.rbh.risk.entity.Role;
import com.rbh.risk.support.SessionFactorySupport;
import com.rbh.risk.util.Constants;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("privilegeDao")
public class PrivilegeDaoImpl extends SessionFactorySupport implements PrivilegeDao {

    @Override
    public void insert(Privilege privilege) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().save(privilege);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(Privilege privilege) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().update(privilege);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(Privilege privilege) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(privilege);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public Privilege getById(Integer privilegeId) throws DataAccessException {
        Privilege result = null;
        try {
            result = (Privilege) super.getSessionFactory().getCurrentSession().get(Privilege.class, privilegeId);
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<Privilege> getByRole(Role role) throws DataAccessException {
        List<Privilege> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(Privilege.class);
            criteria.createAlias("role", "r");
            criteria.add(Restrictions.conjunction()
                    .add(Restrictions.eq("role", role))
                    .add(Restrictions.eq("r.roleStatus", Constants.ENABLED)));
            results = criteria.setCacheable(true).list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<Privilege> getByACL(ACL acl) throws DataAccessException {
        List<Privilege> results = null;
        try{
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(Privilege.class);
            criteria.createAlias("acl", "a");
            criteria.add(Restrictions.conjunction()
                    .add(Restrictions.eq("acl", acl))
                    .add(Restrictions.eq("a.aclStatus", Constants.ENABLED)));
            results = criteria.setCacheable(true).list();
        } catch (Exception ex){
            throw ex;
        }
        return results;
    }

    @Override
    public List<Privilege> getAll() throws DataAccessException {
        List<Privilege> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + Privilege.class.getName());
            results = query.setCacheable(true).list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }
}
